package com.victor.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.victor.dao.PicturebookDao;
import com.victor.entity.Picturebook;
import com.victor.service.PicturebookService;

@Service
public class PictureServiceImpl implements PicturebookService {

	@Autowired
	private PicturebookDao dao;

	@Override
	public int batchDelete(int[] ids) {
		return dao.batchDelete(ids);
	}

	@Override
	public Picturebook findById(Integer pId) {
		return dao.findById(pId);
	}

	@Override
	public boolean update(Picturebook record) {
		return dao.update(record) > 0 ? true : false;
	}

	@Override
	public boolean insertPicture(Picturebook record) {
		return dao.insertPicture(record) > 0 ? true : false;
	}

	@Override
	public List<Picturebook> batchFindByIds(List<String> pidList) {
		return dao.batchFindByIds(pidList);
	}

	@Override
	public List<Map<String, String>> statisticsClick(String pictype, String cid, String picid, String btime,
			String etime) {
		return dao.statisticsClick(pictype, cid, picid, btime, etime);
	}

	@Override
	public boolean addClick(String hPicId) {
		return dao.addClick(hPicId);
	}

	@Override
	public List<Map<String, String>> statisticsBuy(String pictype, String cid, String picid, String btime,
			String etime) {
		return dao.statisticsBuy(pictype, cid, picid, btime, etime);
	}

	@Override
	public List<Map<String, String>> batchFindByIdsm(List<String> pidList, String userid) {
		return dao.batchFindByIdsm(pidList, userid);
	}

	@Override
	public List<Picturebook> findPicture(String pictype) {
		//获取没有关联的绘本 删除掉
		List<Picturebook> picList = dao.findInvalidInfo();
		if (picList != null && !picList.isEmpty()) {
			List<Integer> list = new ArrayList<>();
			picList.stream().forEach(c -> list.add(c.getPId()));
			int[] i = new int[list.size()];
			for (int j = 0; j < list.size(); j++) {
				if (list.get(j) != null) {
					i[j] = list.get(j);
				} else {
					i[j] = 0;
				}
			}
			dao.batchDelete(i);
		}
		return dao.findPicture(pictype);
	}

}
